/************************************************/
/*creator: johan candelaria						*/
/*project name: 								*/
/*notes: 										*/
/*		 										*/
/*												*/
/*contact: gohan127@gmail.com					*/
/*												*/
/*hardware: 	pic pic30f4011					*/
/*				sw1 d1							*/
/*				led1= d2						*/
/*												*/
/*												*/
/*												*/
/*												*/
/************************************************/
	/*includes*/

#include<p30f4011.h>
//#include <string.h>
//#include<uart.h>
#include"wireles.h"

	/*defines*/

#define _BV(bit) (1 << (bit))
#define bit_is_set(register,bit) (register & (1 << (bit)))
#define bit_is_clear(register,bit) (!(register & (1 << (bit)))) 
#define Fcy 30000000

volatile char lastcommand[30]="$0106100000*JJ";
	/*config bits*/
	
	/*interupts*/
  
	/*main program*/



int main (void)
	{
		unsigned long tijmer1;
		TRISDbits.TRISD2=0;
		ADPCFG=0xFFFF; /* Analog ports as Digital I/O  */ 
		InitUART();
		InitLCD();
		InitTimer1();
		tijmer1= GetTime();
		while (80 > GetTimeDifference(tijmer1));
		LATD^= _BV(2);
		tijmer1= GetTime();
		while (80 > GetTimeDifference(tijmer1));
		LATD^= _BV(2);
		tijmer1= GetTime();
		while (80 > GetTimeDifference(tijmer1));
		LATD^= _BV(2);
		tijmer1= GetTime();
		while (80 > GetTimeDifference(tijmer1));
		LATD^= _BV(2);
		tijmer1= GetTime();
		while (80 > GetTimeDifference(tijmer1));
		tijmer1= GetTime();
		LATD^= _BV(2);
		while(1)
		{
			//putsUART1("Johan is een schat!");
			//putcUART1(0x0D);
			//putcUART1(0x0A);
			//putcUART1('U');
			//Int2LCD(tijmer1);
			if (1000 < GetTimeDifference(tijmer1))
			{
				LATD^= _BV(2);
				tijmer1= GetTime();
			}
			if(commandReceived1())
			{
				
				if(lastcommand[5]==1&&lastcommand[6]==0)
				{
					Line1();
					puts2LCD("                ");
					Line1();
					if(lastcommand[7]==0x31)
					{puts2LCD("left,");}
					if(lastcommand[8]==0x31)
					{puts2LCD("right,");}
					if(lastcommand[9]==0x31)
					{puts2LCD("up,");}
					if(lastcommand[10]==0x31)
					{puts2LCD("down");}
					putcUART1('u');
					Int2LCD(lastcommand[11]);
					puts2LCD(",");
					Int2LCD(lastcommand[12]);
					puts2LCD(",");
					Int2LCD(lastcommand[13]);
					puts2LCD(",");
					Int2LCD(lastcommand[14]);
					puts2LCD(",");
					Int2LCD(lastcommand[15]);
					puts2LCD(",");
					Int2LCD(lastcommand[12]);
					puts2LCD(",");
					Int2LCD(lastcommand[13]);
					puts2LCD(",");
					Int2LCD(lastcommand[14]);
					puts2LCD(",");
				}
				putU1('U');
			}
		}
		return 0;
	}
